<template>
<!--添加对话框-->
 <el-dialog 
  title="添加商品信息"
  v-model="addVisible"
  width="70%"  
  @close="closeadd"
 >
 <el-form
    ref="ruleFormRef"
    :model="goodsForm"
    :rules="rule"
  >
    <el-form-item label="商品名称" prop="title">
      <el-input v-model="goodsForm.title" />
    </el-form-item>
    <el-form-item label="商品价格" prop="price">
      <el-input v-model="goodsForm.price" />
    </el-form-item>
    <el-form-item label="商品图片">
        <el-upload
    class="avatar-uploader"
    :action="uploadURL"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
        </el-form-item>
        <el-form-item label="商品详情" >
            <QuillEditor 
	theme="snow" 
	v-model="goodsForm.goodsDetail" />

        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="addshooing">添加商品</el-button>
        </el-form-item>
    </el-form>
 <template #footer>
    <span class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addshooing">确定</el-button>
    </span>
 </template>
 </el-dialog>
    </template>

<script setup>
import { useUsersStore } from "../store/shooing"
import { storeToRefs } from "pinia"
import { reactive, toRefs, ref } from "vue";
import { Plus } from '@element-plus/icons-vue'
import axios from "axios"


let store = useUsersStore()
let { addVisible } = storeToRefs(store)

let reactives = reactive({
    goodsForm: {
        title: '',
        price: 0,
        coverImg: '',
        goodsDetail: ''
    },
    uploadURL: import.meta.env.VITE_APP_URL + "/goods/fileUpload",
    imageUrl: ''

})
let { goodsForm, uploadURL, imageUrl } = toRefs(reactives)
let ruleFormRef = ref()
//关闭对话框
function closeadd() {
    ruleFormRef.value.resetFields()
}
//点击添加商品和确定
function addshooing() {
    ruleFormRef.value.validate((bool) => {
        if (!bool) return
        let params = {
            title: reactives.goodsForm.title,
            price: reactives.goodsForm.price
        }
        axios.post("/goods/add", params).then((res) => {
            store.load()
        })
        store.addVisible = false
    })
}
let rule = reactive({
    title: [{ required: true, message: "请填写商品名称", trigger: 'blur' }],
    price: [{ required: true, message: "请填写商品价格", trigger: 'blur' }]
})
//图片上传成功后的回调
function handleAvatarSuccess(res) {
    reactives.imageUrl = import.meta.env.VITE_APP_URL + res.msg
}
</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>
